package sliding_window;

class Solution {
    //滑动窗口
    public int minSubArrayLen(int target, int[] nums) {
        int sum = 0;
        int len = Integer.MAX_VALUE;// 为0会干扰len的判断
        for (int left = 0, right = 0; right < nums.length; right++) {
            sum += nums[right];// 进窗口
            // 判断
            while (sum >= target) {
                //更新长度
                len = Math.min(len, right - left + 1);
                sum -= nums[left];//出窗口
                left++;
            }
        }
        return len == Integer.MAX_VALUE ? 0 : len;
    }
}
